/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef ERROR_H
#define ERROR_H

#include "utils.h"

#define THROW(ec) \
    throw error_exception(make_error_code((ec)), \
                          __FILE__, __FUNCTION__, __LINE__)
#define THROW_EXTRA(ec, extra_info) \
    throw error_exception(make_error_code((ec)), (extra_info), \
                          __FILE__, __FUNCTION__, __LINE__)

class error_exception : public std::exception {
 public:
  error_exception(utils::error_code ec, 
                  const char* file,
                  const char* function,
                  uint32_t line);

  error_exception(utils::error_code ec,
                  const char* extra_info,
                  const char* file,
                  const char* function,
                  uint32_t line);

  error_exception(utils::error_code ec,
                  const std::string& extra_info,
                  const char* file,
                  const char* function,
                  uint32_t line);

  virtual ~error_exception(void) throw() {}

  const utils::error_code& code(void) const throw() { return ec_; }
  const char* what(void) const throw();

 private:
  void Init(const char* file, const char* function, uint32_t line);

  const char* file_;
  const char* function_;
  uint32_t    line_;
  utils::error_code  ec_;
  std::string extra_info_;
  mutable std::string what_;
};

#endif // ERROR_H

